﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace IndustrialMusicWindowsCSharp {
    public partial class ParseSampleSources : Form {
        public ParseSampleSources() {
            InitializeComponent();
        }

        private void btnSelectSource_Click(object sender, EventArgs e) {
            DialogResult result = sourceFileDialog.ShowDialog();
            if (result == DialogResult.OK) {
                txtSource.Text = sourceFileDialog.FileName;
            }
        }

        private void btnSelectDestination_Click(object sender, EventArgs e) {
            DialogResult result = destinationBrowserDialog.ShowDialog();
            if (result == DialogResult.OK) {
                txtDestination.Text = destinationBrowserDialog.SelectedPath;
            }

        }

        private void btnParse_Click(object sender, EventArgs e) {
            string fileContents = GetFileContents();
            XmlDataDocument dataDocument = GetXmlDocument();

            MatchCollection sourceCollection = ParseFileContents(fileContents);
            
            BuildSourcesDocument(dataDocument, sourceCollection);
            SaveSourcesDocument(dataDocument);
        }

        private XmlDataDocument GetXmlDocument() {
            XmlDataDocument dataDocument = new XmlDataDocument();
            dataDocument.LoadXml("<?xml version='1.0' encoding='utf-8' ?><sources />");

            return dataDocument;
        }

        private string GetFileContents() {
            StreamReader reader = new StreamReader(txtSource.Text.Trim());
            string fileContents = reader.ReadToEnd();

            reader.Close();
            reader.Dispose();

            return fileContents;
        }

        private void SaveSourcesDocument(XmlDataDocument dataDocument) {
            XmlTextWriter writer = new XmlTextWriter(txtDestination.Text + @"\sampleSources.xml", Encoding.UTF8);
            dataDocument.WriteContentTo(writer);
            writer.Flush();
            writer.Close();
        }

        private void BuildSourcesDocument(XmlDataDocument dataDocument, MatchCollection sourceCollection) {
            foreach (Match match in sourceCollection) {
                XmlNode sourceNode = dataDocument.CreateNode(XmlNodeType.Element, "source", string.Empty);
                sourceNode.InnerText = match.Groups[1].Value.Trim();
                dataDocument.DocumentElement.AppendChild(sourceNode);
            }
        }

        private MatchCollection ParseFileContents(string fileContents) {
            Regex sourcesRegEx = new Regex(@"\d{1,4}\s{1}\(\s{0,3}\d{1,4}\)(.+?)\d{1,4}\s{1,4}\d{1,4}\s{1,4}\d{1,4}\s{1,4}\d{1,4}\s{1,4}");
            MatchCollection matches = sourcesRegEx.Matches(fileContents);
            return matches;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }
    }
}
